<?php 
namespace Admin\Controller;
use Think\Controller;

class ArtController extends Controller{

	public function artadd(){

		if(!$_POST){
		$catmodel = D('cat');
		$catdata = $catmodel->select();		
		$this->assign('data',$catdata);
		$this->display();

		}else{

		$artmodel = D('art');
		$_POST['pubtime']=time();
		$_POST['content']=str_replace("\n", '<br>', $_POST['content']);
		$artmodel -> add($_POST);
		$this->redirect('admin/art/artlist');

		}

	}

	public function artlist(){
		$artmodel = D('art');
        $catmodel = D('cat');

		//文章
    	$data = $artmodel -> select();
    	$newdata=array();
    	foreach ($data as $key => $value) {
    		$catname = $catmodel->find($value['cat_id']);
    		$value['cat_id']=$catname['catname'];
            $value['pubtime']=date("Y-m-d",$value['pubtime']);
    		$newdata[]=$value;
    	}


    	$this->assign('art',$newdata);
 
		$this->display();
	}

	public function artdel(){
 		$artmodel = D('art');
 		$data = $artmodel->delete($_GET['art_id']);
 		if ($data) {
 			$this->redirect('admin/art/artlist');
 		}else{
 			echo "代码有问题";
 		}
 		
	}

	public function artupdate(){

		if (!$_POST) {
		
		$catmodel = D('cat');
		$catdata = $catmodel->select();		
		$this->assign('data',$catdata);

		$artmodel = D('art');
		$data = $artmodel->find($_GET['art_id']);
		$this->assign('art',$data);

		$this->display();

		}else{

		$artmodel = D('art');
		$a=$_GET['art_id'];

		$_POST['lastup']=time();
		$artmodel->where("art_id=$a")->save($_POST);
		$this->redirect('admin/art/artlist');
		}
	}

}

?>